package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.admin.dto.BusinessProjectDetail;
import org.linlinjava.litemall.admin.service.AdBusinessProjectService;
import org.linlinjava.litemall.admin.service.LogHelper;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.BusinessProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Daisen.Z
 * @Date: 2021/6/3 16:21
 * @Version: 1.0
 * @Description: 科研业务项目控制层
 */
@RestController
@RequestMapping("/admin/project")
@Validated
public class AdBusinessProjectController {

    private final Log logger = LogFactory.getLog(AdBusinessProjectController.class);


    @Autowired
    private AdBusinessProjectService adBusinessProjectService;

    @RequiresPermissions("admin:project:list")
    @RequiresPermissionsDesc(menu = {"科研业务项目管理", "查询"}, button = "查询")
    @GetMapping("/list")
    public Object list(String projectExecuteYear,String projectName,String projectType,Integer statusAudit,String dutyPersonalName,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        return  adBusinessProjectService.querySelective(projectExecuteYear, projectName,projectType, statusAudit,dutyPersonalName,page, limit, sort, order);
    }

    @RequiresPermissions("admin:project:listByPass")
    @RequiresPermissionsDesc(menu = {"科研业务项目管理", "查询已审核列表"}, button = "查询已审核列表")
    @GetMapping("/listByPass")
    public Object listByPass() {
        return  adBusinessProjectService.queryByPass();
    }

    /**
     * 删除科研项目
     *
     * @param businessProject
     * @return
     */
    @RequiresPermissions("admin:project:delete")
    @RequiresPermissionsDesc(menu = {"科研业务项目管理", "删除"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody BusinessProject businessProject) {
        try {
            Object delete = adBusinessProjectService.delete(businessProject);
            return delete;
        } catch (Exception e) {
            return ResponseUtil.serious();
        }
    }

    /**
     * 查询项目相关成果
     * @param businessProject
     * @return
     */
    @RequiresPermissions("admin:project:getAboutResult")
    @RequiresPermissionsDesc(menu = {"科研业务项目管理", "查询项目相关成果"}, button = "查询项目相关成果")
    @PostMapping("/getAboutResult")
    public Object getAboutResult(@RequestBody BusinessProject businessProject) {
        return adBusinessProjectService.getAboutResult(businessProject.getProjectId());
    }



    // 新增：需要有一个AllInOne的类，包含项目成员，项目附件信息，先添加项目附件，再项目成员，再添加项目成员和项目的关系
    /**
     * 查询项目相关成果
     * @param businessProjectDetail
     * @return
     */
    @RequiresPermissions("admin:project:create")
    @RequiresPermissionsDesc(menu = {"科研业务项目管理", "新增"}, button = "新增")
    @PostMapping("/create")
    public Object create(@RequestBody BusinessProjectDetail businessProjectDetail) {
            return adBusinessProjectService.create(businessProjectDetail);
    }



    // 详情
    @RequiresPermissions("admin:project:detail")
    @RequiresPermissionsDesc(menu = {"科研业务项目管理", "查询项目详情"}, button = "查询项目详情")
    @PostMapping("/detail")
    public Object detail(@RequestBody BusinessProject businessProject) {
        return adBusinessProjectService.detail(businessProject.getProjectId());
    }



    // 更新：修改第一负责人要同步修改项目负责人，添加项目成员时第一负责人只能有一个
    @RequiresPermissions("admin:project:update")
    @RequiresPermissionsDesc(menu = {"科研业务项目管理", "更新"}, button = "更新")
    @PostMapping("/update")
    public Object update(@RequestBody BusinessProjectDetail businessProjectDetail) {
        return adBusinessProjectService.update(businessProjectDetail);
    }

}
